﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Collaboris Ltd." file="EntityReport.cs">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   entity report.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Engine.Report
{
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    /// <summary>
    /// entity report.
    /// </summary>
    [Serializable]
    public class EntityReport : ReportBase<IProgressReport>, IXmlSerializable
    {
        /// <summary>
        /// entity.
        /// </summary>
        private Object entity;

        /// <summary>
        /// The Entity that was processed.
        /// </summary>
        [XmlElement]
        public Object Entity
        {
            get { return this.entity; }

            set { this.entity = value; }
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            string typeAsString = reader.GetAttribute("Type");
            Type type = Type.GetType(typeAsString);

            reader.ReadStartElement();
            this.entity = (Object)new XmlSerializer(type).Deserialize(reader);
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("Type", this.entity.GetType().AssemblyQualifiedName);
            new XmlSerializer(this.entity.GetType()).Serialize(writer, this.entity);
        }
    }
}